from service.utils import config
import cloudscraper
from loguru import logger


# CSFloat
def request_csfloat_list(options={}):
    try:
        url = "https://csfloat.com/api/v1/listings"
        headers = {}
        cookies = {"session": config.get("CSFLOAT", "SESSION")}
        params = {
            "limit": "1",  # 限制找最便宜的
            "sort_by": "lowest_price",  # 按最低价格排序
            "type": "buy_now",  # 现在可以购买
            **options,
        }
        scraper = cloudscraper.create_scraper(
            browser={"browser": "firefox", "platform": "windows", "mobile": False},
            debug=False,
        )
        response = scraper.get(
            url,
            cookies=cookies,
            params=params,
            headers=headers,
            timeout=10000,
        ).json()
        if len(response.get("data", [])) == 0:
            raise Exception("空数据或请求失败！")
        return response
    except Exception as e:
        logger.warning(f"{options.get('item_name', '未知')} CSFLOAT查询失败: [{e}]")
        return {}
